package com.game.raceroom.player.effect
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	
	import com.util.SwfUtil;
	import com.util.Vector;
	import com.game.raceroom.Room;
	import com.game.raceroom.player.Player;
	import com.game.raceroom.player.skill.Skill;
	
	public class CrashList
	{
		var room:Room;
		var clist:Vector=new Vector;
		public function CrashList(rm:Room)
		{
			room=rm;
			loadMovie();
		}
        protected var myLoader:Loader;
         protected var myLoader1:Loader;
		public function loadMovie():void{
			myLoader=new Loader();
		    myLoader.contentLoaderInfo.addEventListener(
  	        Event.COMPLETE,function (e:Event):void{initEftOk(e)});
            myLoader.loadBytes(SwfUtil.getEffectSwfStream(SwfUtil.EFT_NAME_CRASH));
			myLoader1=new Loader();
		    myLoader1.contentLoaderInfo.addEventListener(
  	        Event.COMPLETE,function (e:Event):void{initEftOk1(e)});
            myLoader1.loadBytes(SwfUtil.getEffectSwfStream(SwfUtil.EFT_NAME_CRASH));
      
		}
		public var mc0:MovieClip; 
		public var mc1:MovieClip; 
		var scaleNum:Number = 1;
        public function initEftOk(e:Event):void{
            mc0 = myLoader.content as MovieClip;
            mc0.visible=false;
        } 
        public function initEftOk1(e:Event):void{
            mc1= myLoader.content as MovieClip;
            mc1.visible=false;
        } 
        public function addCrashPlayer(atk:int,def:int,lv:int,decblood:int):void{
        	for(var i:int=0;i<clist.size();i++){
        		var c:CrashInfo=(CrashInfo)(clist.getObjById(i));
//        		if(atk==c.attacker && def==c.defender){
//        			if(c.level<lv)c.level=lv;
//        		    return;
//        		}
        		if(atk==c.defender){
        			if(def==c.attacker){
	        			if(c.level<lv)c.level=lv;
	        			c.defBlood=decblood;
	        		    return;
        		 	}
        		}

        	}
        	    var c1:CrashInfo=new CrashInfo;
        		c1.attacker=atk;
        		c1.defender=def;
        		c1.level=lv;
        		c1.atkBlood=decblood;
        		clist.put(c1);
        }
        public var mc0_free:Boolean=true;
        public var mc1_free:Boolean=true;
        public function freeMc(m:MovieClip){
        	if(m==mc0){
        	    mc0_free=true;
        	    mc0.alpha=1;
        	    mc0.scaleX=1;
        	    mc0.scaleY=1;
        	}else if(m==mc1){
        		mc1_free=true;
        	    mc1.alpha=1;
        	    mc1.scaleX=1;
        	    mc1.scaleY=1;
        	}
        }
        public function flush(){
        	
	        for(var i:int=0;i<clist.size();i++){
	        	
        		var c:CrashInfo=(CrashInfo)(clist.getObjById(i));
       		    var attacker:Player=(Player)(room.players.getObjByKey(c.attacker));
       		    var defender:Player=(Player)(room.players.getObjByKey(c.defender));
            	if(attacker==room.actorContrl){
	            	if(c.atkBlood>c.defBlood){
	            	    if(c.level>0 || c.atkBlood-c.defBlood>150){
	            			if(attacker.combo<Player.MAX_COMBO)attacker.combo++;
	            		
							for(var i:int=1;i<=8;i++){
				    	   	   var skill:Skill=(Skill)(room.skillBar.skills.getObjByKey(i));
				    	       if(!skill)continue;
				    	       if(skill.skillType==Skill.SKILL_TYPE_STRICK){
				    	                skill.coldDown();
				    	                break;
				    	       }
				   			}
				   			room.actorContrl.actorDisp.setCombo(attacker.combo);
			   			}
		    		}else{
		    			attacker.combo=0;
		    			room.actorContrl.actorDisp.setCombo(attacker.combo);
		    		}
		    		
	            }else
	            if(defender==room.actorContrl){
	            	if(c.atkBlood<c.defBlood){
	            		if(c.level>0 || c.defBlood-c.atkBlood>150){
	            			if(defender.combo<Player.MAX_COMBO)defender.combo++;
	            		
							for(var i:int=1;i<=8;i++){
				    	   	   var skill:Skill=(Skill)(room.skillBar.skills.getObjByKey(i));
				    	        if(!skill)continue;
				    	       if(skill.skillType==Skill.SKILL_TYPE_STRICK){
				    	                skill.coldDown();
				    	                break;
				    	       }
				   			}
				   			room.actorContrl.actorDisp.setCombo(attacker.combo);
			   			}
		    		}else{
		    			defender.combo=0;
		    			room.actorContrl.actorDisp.setCombo(attacker.combo);
		    		}
	            }
	            
                if(!mc0_free && !mc1_free)continue;
            
	       		var x:Number = attacker._x;
	       		var y:Number =  attacker._y;
	       		var x1:Number = defender._x;
	       		var y1:Number =  defender._y;
				var width:Number = Math.abs(x-x1);
				var height:Number = Math.abs(y-y1);
				
				var startx:Number = x;
				if(x > x1){
					startx = x1;
				}
				
				var starty:Number = y;
				if(y > y1){
					starty = y1;
				}
				startx+=width/2;
				starty+=height/2;
				
				var ce:CrashEffect = null;
				if(mc0_free){
					mc0_free=false;
					ce =new CrashEffect(startx,starty,this.room,c.level,this,mc0);
	 
					room.addEffect(ce);
				}
				else{
					mc1_free=false;
					ce=new CrashEffect(startx,starty,this.room,c.level,this,mc1);
	 
					room.addEffect(ce);					
				}
			
	        }
 
        	clist.clear();
        }
	}
}